package dataStore;

import java.rmi.RemoteException;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.Queue;

import tool.MysqlApi;
import dataStruct.DataStoreTask;
import dataStruct.DataStoreTaskBag;
import dataStruct.EnumHeartBeatType;
import dataStruct.FtpFileDowndedCount;

public class SlaverDataStoreApi implements SlaverDataStoreApiInterface{
	public SlaverDataStoreApi( 
			Queue<String> dataStoreTaskSlaverChunkQueue,
			Map<String, DataStoreTaskBag> dataStoreTaskSlaverChunkMap, 
			Map<String,   FtpFileDowndedCount> ftpFileDownedSizeMap
			)throws SQLException
			{
				SlaverDataStoreApi.dataStoreTaskSlaverChunkMap = dataStoreTaskSlaverChunkMap;
				SlaverDataStoreApi.dataStoreTaskSlaverChunkQueue = dataStoreTaskSlaverChunkQueue;
				SlaverDataStoreApi.ftpFileDownedSizeMap = ftpFileDownedSizeMap;
				state = MysqlApi.get_statement();
			}
	
	/****************************************slaver data storeApi**************************************************************/
	@Override
	public DataStoreTask get_data_store_task_bag(String dataStoreTaskId, Integer slaverId) throws RemoteException {
		// TODO Auto-generated method stub
		
		//debug
		System.out.println(String.format("dataStoreTaskId = %s \tint slaverId = %d(SlaverDataStoreApi.java)", dataStoreTaskId, slaverId));
		
		DataStoreTaskBag dataStoreTaskBag = dataStoreTaskSlaverChunkMap.get(dataStoreTaskId);

		dataStoreTaskBag.slaverId = slaverId;
		dataStoreTaskBag.startTime = new Long(System.currentTimeMillis());
		System.out.println(String.format("dataStoreTask.offset=%s, dataStoreTask.size=%s," +
				" dataStoreTask.slaverChunkId=%s, ftpFilePath = %s(SlaverDataStoreApi.java)", 
				dataStoreTaskBag.dataStoreTask.offset, 
				dataStoreTaskBag.dataStoreTask.size,
				dataStoreTaskBag.dataStoreTask.chunkId,
				 dataStoreTaskBag.dataStoreTask.ftpServerInfo.ftpServerPath));
		
		return dataStoreTaskBag.dataStoreTask;
	}

	public Boolean finish_download_chunk(String dataStoreTaskId)
			throws RemoteException, SQLException {
		//debug
		System.out.println(String.format("\na===================================(SlaverDataStoreApi.java)\n"));
		
		DataStoreTaskBag dataStoreTaskBag = dataStoreTaskSlaverChunkMap.get(dataStoreTaskId);

		//debug
		System.out.println(String.format("\nb===================================(SlaverDataStoreApi.java)\n"));
		
		MysqlApi.update_slaverId_slaverChunk(state, dataStoreTaskId, dataStoreTaskBag.slaverId, 	"1");
		
		//debug
		System.out.println(String.format("\nc===================================(SlaverDataStoreApi.java)\n"));
		
		if(MysqlApi.update_slaverCount_chunkInfo(state, dataStoreTaskBag.dataStoreTask.chunkId,  1) == false)
		{
			System.out.println(String.format("\nError: MysqlApi.update_slaverCount_chunkInfo(SlaverDataStoreApi.java)\n"));
		}
		Boolean ret = false;
		
		//debug
		System.out.println(String.format("\n1===================================(SlaverDataStoreApi.java)\n"));
		
		 FtpFileDowndedCount  remainSize = ftpFileDownedSizeMap.get(dataStoreTaskBag.dataStoreTask.ftpServerInfo.ftpServerPath);
		 remainSize.remainSize -= dataStoreTaskBag.dataStoreTask.size;
		if(remainSize.remainSize <= 0)
		{		
			//debug
			System.out.println(String.format("\n2===================================(SlaverDataStoreApi.java)\n"));
			
			 ftpFileDownedSizeMap.remove(dataStoreTaskBag.dataStoreTask.ftpServerInfo.ftpServerPath);
			 ret = true;
		}
		
		//debug
		System.out.println(String.format("\n3===================================(SlaverDataStoreApi.java)\n"));
		
		dataStoreTaskSlaverChunkMap.remove(dataStoreTaskId);
		
		//debug
		System.out.println(String.format("\n------------------slaver finished  download chunk dataStoreTaskId = %s-------------------------------(SlaverDataStoreApi.java)\n",
				dataStoreTaskId));
		
		return ret;
	}
	public void fail_download_chunk(String dataStoreTaskId)
			throws RemoteException {
		dataStoreTaskSlaverChunkQueue.add(dataStoreTaskId);
		
		//debug
		System.out.println(String.format("\n------------------slaver failed  download chunk dataStoreTaskId = %s and add to dataStoreTaskSlaverChunkQueue download again-------------------------------(SlaverDataStoreApi.java)\n",
				dataStoreTaskId));
	}
	
	@Override
	public void finish_upload_chunk(String dataStoreTaskId)
			throws RemoteException {
		DataStoreTaskBag dataStoreTaskBag = dataStoreTaskSlaverChunkMap.get(dataStoreTaskId);
		if(dataStoreTaskBag.isSlaverDown == true)
		{
			dataStoreTaskBag.isSlaverDown = false;
			dataStoreTaskBag.slaverId = new Integer(0);
			dataStoreTaskBag.startTime = new Long(0);
			dataStoreTaskBag.dataStoreTask.heartBeatType =EnumHeartBeatType.RESPONSE_UPLOAD_FILE_SUCCESS ;
			dataStoreTaskSlaverChunkQueue.add(dataStoreTaskId);
			//debug
			System.out.println(String.format("\n------------------chunk finished  upload  then add to  dataStoreTaskSlaverChunkQueue to downloaddataStoreTaskId = %s----(SlaverDataStoreApi.java)\n",
					dataStoreTaskId));
		}
		else
		{
		dataStoreTaskSlaverChunkMap.remove(dataStoreTaskId);
		//debug
		System.out.println(String.format("\n------------------slaver finished  upload chunk dataStoreTaskId = %s---(SlaverDataStoreApi.java)\n",
				dataStoreTaskId));
		}
	}
	public void fail_upload_chunk(String dataStoreTaskId)
			throws RemoteException {
		dataStoreTaskSlaverChunkQueue.add(dataStoreTaskId);
		
		//debug
		System.out.println(String.format("\n------------------slaver failed  upload chunk dataStoreTaskId = %s and add to dataStoreTaskSlaverChunkQueue upload again-------------------------------(SlaverDataStoreApi.java)\n",
				dataStoreTaskId));
	}
	static private Map<String, DataStoreTaskBag> dataStoreTaskSlaverChunkMap;
	static private Queue<String> dataStoreTaskSlaverChunkQueue;
	 private static Map<String,  FtpFileDowndedCount> ftpFileDownedSizeMap;
	static private Statement state;
}
